//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shp.Runtime.Sitka.ServiceAgent.Managers;
using Shp.Runtime.Common;
using Shp.Runtime.Sitka.ServiceAgent.Entities;
using Shp.Runtime.Sitka.ServiceAgent;

namespace LitwareHR.Portfolio.DataModel.Sitka.Tests
{
    [TestClass]
    public class ProvisioningDataModelFixture
    {
        private static Guid tenantId = Guid.NewGuid();
		private static Guid workflowShortId = Guid.NewGuid();
		private static Guid workflowLongId = Guid.NewGuid();
        private const string tenantAlias = "MockAlias";
        private const string tenantName = "MockTenant";

        private static TestableRepositoryFactory factory = new TestableRepositoryFactory(tenantId);

        [ClassInitialize]
        public static void Initialize(TestContext testContext)
        {
        }

        [TestMethod]        
        public void ShouldPerformModulesProvisioning()
        {
            TestableRepositoryFactory factory = new TestableRepositoryFactory(tenantId);
            ProvisioningDataModel dataModel = new ProvisioningDataModel(factory);

            dataModel.PerformModulesProvisioning(tenantId, tenantName, tenantAlias, workflowShortId, workflowLongId);

            using (Repository<GenericExtensibleEntity> repository = factory.MetadataRepository)
            {
                GenericExtensibleEntity tenantInstance = repository.GetById(tenantId);
                Assert.IsNotNull(tenantInstance);
                Assert.AreEqual(tenantInstance.Fields.Count, 4);
                Assert.AreEqual(tenantInstance.GetFieldValue("Alias"), tenantAlias);

                factory.DeleteMetadataContainer();
            }

            Assert.IsTrue(ContainerManager.ContainerExistsForTenant(tenantId.ToString()));
            factory.DeleteTenantContainer();
        }

		[TestMethod]
		public void ShouldInsertWorkflowDef()
		{
			TestableRepositoryFactory factory = new TestableRepositoryFactory(tenantId);
			ProvisioningDataModel dataModel = new ProvisioningDataModel(factory);

			dataModel.PerformModulesProvisioning(tenantId, tenantName, tenantAlias, workflowShortId, workflowLongId);

			using (Repository<GenericExtensibleEntity> repository = factory.MetadataRepository)
			{
				GenericExtensibleEntity workflowInstance = repository.GetById(workflowShortId);
				Assert.IsNotNull(workflowInstance);
				Assert.AreEqual(Mocks.MockWorkflow.GetShortWorkflowXomlMock(), workflowInstance.GetFieldValue("Xoml"));
				

				factory.DeleteMetadataContainer();
			}

			Assert.IsTrue(ContainerManager.ContainerExistsForTenant(tenantId.ToString()));
			factory.DeleteTenantContainer();
		}

        private const string SilverligthAccountId = "33721";
        private const string SilverligthAccountKey = "0dcc5f96ee99b7b57fe7b488b1a16d38";

        [TestMethod]
        public void ShouldProvisionSilverlightStreaming()
        {
            TestableRepositoryFactory factory = new TestableRepositoryFactory(tenantId);
            ProvisioningDataModel dataModel = new ProvisioningDataModel(factory);

            dataModel.PerformModulesProvisioning(tenantId, tenantName, tenantAlias, workflowShortId, workflowLongId);
            dataModel.ProvisionSilverlightStreaming(tenantId, SilverligthAccountId, SilverligthAccountKey);

            using (Repository<GenericExtensibleEntity> repository = factory.MetadataRepository)
            {
                GenericExtensibleEntity tenantInstance = repository.GetById(tenantId);
                Assert.IsNotNull(tenantInstance);
                Assert.AreEqual(6, tenantInstance.Fields.Count);
                Assert.AreEqual(tenantAlias, tenantInstance.GetFieldValue("Alias"));
                Assert.AreEqual(SilverligthAccountId, tenantInstance.GetFieldValue("SilverlightAccountId"));
                Assert.AreEqual(SilverligthAccountKey, tenantInstance.GetFieldValue("SilverlightAccountKey"));


                factory.DeleteMetadataContainer();
            }

            Assert.IsTrue(ContainerManager.ContainerExistsForTenant(tenantId.ToString()));
            factory.DeleteTenantContainer();
        }

        [ClassCleanup]
        public static void CleanUp()
        {        
            factory.DeleteMetadataContainer();
        }
    }
}
